﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quick.CodeGenerator.Model;

namespace Quick.CodeGenerator.CodeTemplate
{
    public class IDomainTemplate : ITemplate
    {
        /// <summary>
        /// 生成模版
        /// </summary>
        /// <param name="setting"></param>
        /// <returns></returns>
        public string GenerateTemplate(CodeGeneratorSetting setting, string tableName, List<Model.ColumnInfo> columns)
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.AppendLine("/****************");
            strBuilder.AppendLine($"* Creat Date: {DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss")}");
            strBuilder.AppendLine($"* Creator: {setting.Creator}");
            strBuilder.AppendLine("*****************/");
            strBuilder.AppendLine("using System;");
            strBuilder.AppendLine("using System.Collections.Generic;");
            strBuilder.AppendLine("using System.Linq;");
            strBuilder.AppendLine("using System.Text;");
            strBuilder.AppendLine("using System.Threading.Tasks;");
            strBuilder.AppendLine("using Utility.Contracts;");
            strBuilder.AppendLine("using Utility.Data.DbClient;");
            strBuilder.AppendLine($"namespace {setting.Project}.{setting.IDomainNamespace}");
            strBuilder.AppendLine("{");
            strBuilder.AppendLine("    /// <summary>");
            strBuilder.AppendLine("    /// 业务接口操作类：" + tableName);
            strBuilder.AppendLine("    /// CodeGenerator中的类是自动创建生成,正常不应该进行修改,否则下次生成可能会被覆盖");
            strBuilder.AppendLine("    /// </summary>");
            strBuilder.AppendLine($"    public partial interface I{tableName} : IDependency");
            strBuilder.AppendLine("    {");

            strBuilder.AppendLine($"        /// <summary>");
            strBuilder.AppendLine($"        /// 插入或者更新,");
            strBuilder.AppendLine($"        /// 根据主键进行查找数据. 没有则插入,有则更新,");
            strBuilder.AppendLine($"        /// </summary>");
            strBuilder.AppendLine($"        /// <param name=\"model\"></param>");
            strBuilder.AppendLine($"        /// <returns></returns>");
            strBuilder.AppendLine($"        int AddOrUpdate({setting.Project}.{setting.EntityNamespace}.{tableName} model);");
            strBuilder.AppendLine($"        /// <summary>");
            strBuilder.AppendLine($"        /// 插入或者更新,");
            strBuilder.AppendLine($"        /// 根据主键进行查找数据. 没有则插入,有则更新,");
            strBuilder.AppendLine($"        /// </summary>");
            strBuilder.AppendLine($"        /// <param name=\"models\"></param>");
            strBuilder.AppendLine($"        /// <param name=\"action\">");
            strBuilder.AppendLine($"        /// 处理进度");
            strBuilder.AppendLine($"        /// string: 当前处理的类型");
            strBuilder.AppendLine($"        /// int1: 总数据量");
            strBuilder.AppendLine($"        /// int2: 当前处理量");
            strBuilder.AppendLine($"        /// </param>");
            strBuilder.AppendLine($"        /// <returns></returns>");
            strBuilder.AppendLine($"        int AddOrUpdate(List<{setting.Project}.{setting.EntityNamespace}.{tableName}> models, Action<string, int, int> action = null);");
            strBuilder.AppendLine($"        /// <summary>");
            strBuilder.AppendLine($"        /// 添加Model list");
            strBuilder.AppendLine($"        /// </summary>");
            strBuilder.AppendLine($"        /// <param name=\"models\"></param>");
            strBuilder.AppendLine($"        /// <param name=\"action\">");
            strBuilder.AppendLine($"        /// 处理进度");
            strBuilder.AppendLine($"        /// string: 当前处理的类型");
            strBuilder.AppendLine($"        /// int1: 总数据量");
            strBuilder.AppendLine($"        /// int2: 当前处理量");
            strBuilder.AppendLine($"        /// </param>");
            strBuilder.AppendLine($"        /// <returns></returns>");
            strBuilder.AppendLine($"        int Add(List<{setting.Project}.{setting.EntityNamespace}.{tableName}> models, Action<string, int, int> action = null);");
            strBuilder.AppendLine($"        /// <summary>");
            strBuilder.AppendLine($"        /// 增加一条数据");
            strBuilder.AppendLine($"        /// </summary>");
            strBuilder.AppendLine($"        int Add({setting.Project}.{setting.EntityNamespace}.{tableName} model);");
            strBuilder.AppendLine($"        /// <summary>");
            strBuilder.AppendLine($"        /// 更新一条数据[需要Model的 主键设定值]");
            strBuilder.AppendLine($"        /// </summary>");
            strBuilder.AppendLine($"        int Update({setting.Project}.{setting.EntityNamespace}.{tableName} model);");
            strBuilder.AppendLine($"        /// <summary>");
            strBuilder.AppendLine($"        /// 更新 Model list");
            strBuilder.AppendLine($"        /// </summary>");
            strBuilder.AppendLine($"        /// <param name=\"models\"></param>");
            strBuilder.AppendLine($"        /// <param name=\"action\">");
            strBuilder.AppendLine($"        /// 处理进度");
            strBuilder.AppendLine($"        /// string: 当前处理的类型");
            strBuilder.AppendLine($"        /// int1: 总数据量");
            strBuilder.AppendLine($"        /// int2: 当前处理量");
            strBuilder.AppendLine($"        /// </param>");
            strBuilder.AppendLine($"        /// <returns></returns>");
            strBuilder.AppendLine($"        int Update(List<{setting.Project}.{setting.EntityNamespace}.{tableName}> models, Action<string, int, int> action = null);");
            strBuilder.AppendLine($"        /// <summary>");
            strBuilder.AppendLine($"        /// 删除一条数据[需要Model的 主键设定值]");
            strBuilder.AppendLine($"        /// </summary>");
            strBuilder.AppendLine($"        int Delete({setting.Project}.{setting.EntityNamespace}.{tableName} model);");
            strBuilder.AppendLine($"        /// <summary>");
            strBuilder.AppendLine($"        /// 删除一条数据[需要Model的 主键设定值]");
            strBuilder.AppendLine($"        /// </summary>");
            strBuilder.AppendLine($"        /// <param name=\"action\">");
            strBuilder.AppendLine($"        /// <param name=\"models\"></param>");
            strBuilder.AppendLine($"        /// 处理进度");
            strBuilder.AppendLine($"        /// string: 当前处理的类型");
            strBuilder.AppendLine($"        /// int1: 总数据量");
            strBuilder.AppendLine($"        /// int2: 当前处理量");
            strBuilder.AppendLine($"        /// </param>");
            strBuilder.AppendLine($"        int Delete(List<{setting.Project}.{setting.EntityNamespace}.{tableName}> models, Action<string, int, int> action = null);");
            strBuilder.AppendLine($"        /// <summary>");
            strBuilder.AppendLine($"        /// 查找数据[根据设置的Model值作为查询条件,进行选择]");
            strBuilder.AppendLine($"        /// </summary>");
            strBuilder.AppendLine($"        /// <param name=\"model\"></param>");
            strBuilder.AppendLine($"        /// <param name=\"isClearChanges\">是否清除项次改变值</param>");
            strBuilder.AppendLine($"        /// <returns></returns>");
            strBuilder.AppendLine($"        {setting.Project}.{setting.EntityNamespace}.{tableName} GetModel({setting.Project}.{setting.EntityNamespace}.{tableName} model, bool isClearChanges = true);");
            strBuilder.AppendLine($"        /// <summary>");
            strBuilder.AppendLine($"        /// 获取数据列");
            strBuilder.AppendLine($"        /// </summary>");
            strBuilder.AppendLine($"        List<{setting.Project}.{setting.EntityNamespace}.{tableName}> GetList();");
            strBuilder.AppendLine($"        /// <summary>");
            strBuilder.AppendLine($"        /// 获取数据列,根据设定的Model值进行查询");
            strBuilder.AppendLine($"        /// </summary>");
            strBuilder.AppendLine($"        List<{setting.Project}.{setting.EntityNamespace}.{tableName}> GetList({setting.Project}.{setting.EntityNamespace}.{tableName} model);");
            strBuilder.AppendLine($"        /// <summary>");
            strBuilder.AppendLine($"        /// 获取数据列,根据设定的Model值进行查询");
            strBuilder.AppendLine($"        /// </summary>");
            strBuilder.AppendLine($"        /// <param name=\"model\"></param>");
            strBuilder.AppendLine($"        /// <param name=\"orderBy\">MS-SQLSERVER 必填字段,其它数据库不要求</param>");
            strBuilder.AppendLine($"        /// <param name=\"totalCount\"></param>");
            strBuilder.AppendLine($"        /// <param name=\"pageIndex\"></param>");
            strBuilder.AppendLine($"        /// <param name=\"pageSize\"></param>");
            strBuilder.AppendLine($"        /// <returns></returns>");
            strBuilder.AppendLine($"        List<{setting.Project}.{setting.EntityNamespace}.{tableName}> GetList({setting.Project}.{setting.EntityNamespace}.{tableName} model, string orderBy, out int totalCount, int pageIndex = 1, int pageSize = 20);");
            strBuilder.AppendLine($"        /// <summary>");
            strBuilder.AppendLine($"        /// 获取数据列,根据自定义条件进行查询");
            strBuilder.AppendLine($"        /// </summary>");
            strBuilder.AppendLine($"        /// <param name=\"sqlWhere\"></param>");
            strBuilder.AppendLine($"        /// <param name=\"orderBy\">MS-SQLSERVER 必填字段,其它数据库不要求</param>");
            strBuilder.AppendLine($"        /// <param name=\"totalCount\"></param>");
            strBuilder.AppendLine($"        /// <param name=\"pageIndex\"></param>");
            strBuilder.AppendLine($"        /// <param name=\"pageSize\"></param>");
            strBuilder.AppendLine($"        /// <param name=\"cmdParms\"></param>");
            strBuilder.AppendLine($"        /// <returns></returns>");
            strBuilder.AppendLine($"        List<{setting.Project}.{setting.EntityNamespace}.{tableName}> GetList(string sqlWhere, string orderBy, out int totalCount, int pageIndex = 1, int pageSize = 20, params DBParameter[] cmdParms);");
            strBuilder.AppendLine($"        /// <summary>");
            strBuilder.AppendLine($"        /// 是否存在指定条件的数据");
            strBuilder.AppendLine($"        /// </summary>");
            strBuilder.AppendLine($"        bool Exists({setting.Project}.{setting.EntityNamespace}.{tableName} model);");
            //strBuilder.AppendLine($"        /// <summary>");
            //strBuilder.AppendLine($"        /// 是否存在指定条件的数据[需要Model的 主键设定值]");
            //strBuilder.AppendLine($"        /// </summary>");
            //strBuilder.AppendLine($"        bool ExistsPK({setting.Project}.{setting.ModelNamespace}.{tableName} model);");
            strBuilder.AppendLine($"        /// <summary>");
            strBuilder.AppendLine($"        /// 软删除数据");
            strBuilder.AppendLine($"        /// </summary>");
            strBuilder.AppendLine($"        int SoftDelete({setting.Project}.{setting.EntityNamespace}.{tableName} model);");

            strBuilder.AppendLine("    }");
            strBuilder.AppendLine("}");
            return strBuilder.ToString();
        }
    }
}